package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：seanyang
 * @date ：Created in 2019/6/18
 * @description ：检查项业务实现类
 * @version: 1.0
 */
@Service
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {

	@Autowired
	private CheckItemDao checkItemDao;

	@Transactional
	@Override
	public void add(CheckItem checkItem) {
		log.debug("CheckItemServiceImpl:{}"+checkItem);
		checkItemDao.add((checkItem));
	}

	/**
	 * 分页根据条件进行查询
	 * @param queryPageBean 查询参数
	 * @return
	 */
	@Override
	public PageResult pageQuery(QueryPageBean queryPageBean) {
		PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
	Page<CheckItem> page=	checkItemDao.selectByCondition(queryPageBean.getQueryString());
		return new PageResult(page.getTotal(),page.getResult());
	}

	@Override
	public void delete(Integer id) {
		Long count = checkItemDao.countCheckItemsById(id);
		if (count>0){
			throw new RuntimeException("当前检查项有数据，不能删除！！！");
		}
		checkItemDao.delete(id);
	}

	@Override
	public CheckItem findById(Integer id) {
		return checkItemDao.findById(id);
	}

	@Override
	public void update(CheckItem checkItem) {
		checkItemDao.update(checkItem);
	}

	@Override
	public List<CheckItem> findAll() {
		return checkItemDao.findAll();
	}
}
